/*****************   CJLib   ********************************
**
** Title  :     c.wimpc
**
*****************************************************************/



#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string.h>



/* from osLib */
#include "OSLib:taskmanager.h"
#include "OSLib:wimp.h"
#include "OSLib:toolbox.h"
#include "OSLib:window.h"


// private headers
#include "os.h"
#include "tbox.h"
#include "wimpc.h"


/* return the task handle of the first task matching Name */
wimp_t CJL_FindTask (char* name)
{
  taskmanager_task task;
  int context = 0;
  char* end;
  int result = 1;
  int len;

  if (name == NULL) return 0;
  if (*name == 0x00) return 0;

  len = strlen (name);
  do
  {
    context = taskmanager_enumerate_tasks (context, &task, sizeof (task), &end);
    if (task.name != NULL ) result = strncmp (name, task.name, len);
  } while (context >= 0 && (result != 0));

  // return 0 if no match found
  if (result == 0) return task.task;
  else return (0);
}



/******************************************
*
* function:    force_redraw_raw
*
* description: forces a redraw of the visible area
*
* input:       win - wimp window handle
*
*******************************************/

void  CJL_ForceWindowRedraw ( wimp_w win )

{
  wimp_window_state state;
  int x0, x1, y0, y1;

  state.w = win;
  wimp_get_window_state ( &state );
  x0 = state.xscroll;
  y0 = state.visible.y0 - state.visible.y1 + state.yscroll;
  x1 = state.visible.x1 - state.visible.x0 + state.xscroll;
  y1 = - state.yscroll;
  wimp_force_redraw ( win, x0, y0, x1, y1 );

  return;
}





/*****************************************************************
 *  Start a little above and left of centre the window and move
 *  each subsequent window down and right by 48 OS units, so they
 *  would be stacked rather than one exactly on top of the other.
 *  After moving down and right seven times,  cycle through the
 *  positions again.
*****************************************************************/

#define ORIGIN_OFFSET_OS 48    /* As per style guide */

void CJL_initial_show_object_centred_with_offset ( toolbox_o win_id, int *win_idx )

{
  wimp_window_state state;
  int wid, ht;
  vdu_variables vdu;
  toolbox_position pos;
  int winidx;
  wimp_w winh;


  winh = window_get_wimp_handle (0, win_id);
  /* get window state */
  state.w = winh;
  wimp_get_window_state ( &state );
  /* get visible extent */
  wid = state.visible.x1 - state.visible.x0;
  ht = state.visible.y1 - state.visible.y0;

  CJL_get_vdu_details (&vdu);
  winidx = *win_idx;
  pos.top_left.x = (vdu.xwin_limit >> 1) - (wid >> 1)
                                         - (3 * ORIGIN_OFFSET_OS)
                                         + (winidx * ORIGIN_OFFSET_OS);

  pos.top_left.y = (vdu.ywin_limit >> 1) + (ht >> 1)
                                         + (3 * ORIGIN_OFFSET_OS)
                                         - (winidx * ORIGIN_OFFSET_OS);

  winidx == 6 ? winidx = 0 : winidx++;
  *win_idx = winidx;
  toolbox_show_object ( 0,
                        win_id,
                        toolbox_POSITION_TOP_LEFT,
                        &pos,
                        toolbox_NULL_OBJECT,
                        toolbox_NULL_COMPONENT ) ;

  return;
}





